using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Edge.Common.Web.Controls.Ext
{
    [
     DefaultProperty(""),
     ParseChildren(false),
     ToolboxData("<{0}:DropDownList runat=\"server\"/>")
     ]

    public class DropDownList : System.Web.UI.WebControls.DropDownList
    {

        //variable
        private const string KEY_IS_LABLE = "this is label - key in view state";
        private const string KEY_TEXT = "text key in view state";
        private const string KEY_DELETED_VALUE = "key in view state about value was deleted";

        public string DeletedMessageId
        {
            get { return this.ClientID + "_del"; }
        }

        private string FunctionName
        {
            get { return this.ClientID + "_valid()"; }
        }

        public bool IsLabel
        {
            get
            {
                object state = ViewState[KEY_IS_LABLE];
                return state != null ? (bool)state : false;
            }
            set
            {
                ViewState[KEY_IS_LABLE] = value;
            }
        }

        public string Text
        {
            get
            {
                object o = ViewState[KEY_TEXT];
                return (string)o;
            }
            set
            {
                ViewState[KEY_TEXT] = value;
            }
        }

        public bool SelectedValueDeleted
        {
            get
            {
                if (ViewState[KEY_DELETED_VALUE] == null) ViewState[KEY_DELETED_VALUE] = false;
                return (bool)ViewState[KEY_DELETED_VALUE];
            }
            set
            {
                ViewState[KEY_DELETED_VALUE] = value;
            }
        }

        protected override void OnPreRender(System.EventArgs e)
        {
            if (SelectedValueDeleted && !IsLabel)
            {
                if (this.Attributes["onchange"] == null) this.Attributes["onchange"] = "";
                if (this.Attributes["onchange"].IndexOf(FunctionName) < 0)
                    this.Attributes["onchange"] = this.Attributes["onchange"] + " " + FunctionName + "; ";
            }
            base.OnPreRender(e);
        }

        /// <summary> 
        /// display text box or label
        /// </summary>
        protected override void Render(HtmlTextWriter output)
        {

            if (!IsLabel)
            {
                base.Render(output);
            }
            else
            {

                Label lb = new Label();
                lb.ID = this.ClientID;
                lb.Text = GetLabelText();
                lb.ToolTip = ToolTip;
                lb.RenderControl(output);
            }

            if (!SelectedValueDeleted) return;

            Label lb2 = new Label();
            lb2.ID = DeletedMessageId;
            lb2.CssClass = "validator";
            lb2.Text = " was deleted";
            lb2.RenderControl(output);

            if (IsLabel) return;

            string scrM = " <script language=\"javascript\" type=\"text/javascript\">function {0}{{ var ob = document.getElementById('{1}'); var mes = document.getElementById('{3}'); if (ob.options[ob.selectedIndex].value == {2}) mes.style.display = ''; else mes.style.display = 'none'; }}</script>";

            output.Write(string.Format(scrM, FunctionName, ClientID, SelectedValue, DeletedMessageId));

        }

        protected string GetLabelText()
        {
            string text = Text;

            if ((text == null || text == string.Empty) && SelectedItem != null)
                text = this.SelectedItem.Text;

            if (text == null || text.Trim().Length == 0)
                text = "&nbsp;";
            return text;

        }

    }
}
